package it.polito.connectedcar.observer;

import it.polito.appeal.traci.SumoTraciConnection;
import it.polito.appeal.traci.VehicleLifecycleObserver;
import it.polito.appeal.traci.connectedcar.sumo.SUMOVehicle;

public class ConnectedCarVehicleLifecycleObserver implements VehicleLifecycleObserver {

	private ConnectedCarLifeCycleObserver connectedCarsObserver;
	private SumoTraciConnection connection;
	
	private static final String CONNECTED_VEHICLE_TYPES="connectedCarType";
	public ConnectedCarVehicleLifecycleObserver(SumoTraciConnection connection,ConnectedCarLifeCycleObserver connectedCarsObserver)
	{
		this.connection=connection;
		this.connectedCarsObserver= connectedCarsObserver;
	}
	
	@Override
	public void vehicleDeparted(SUMOVehicle vehicle) {
		System.out.println("vehicle departed: "+vehicle.getID());
		if(this.connection!=null && this.connectedCarsObserver!=null)
		{
			this.connectedCarsObserver.addVehicle(vehicle);
			/*
			if(vehicle.getVehicleType().equals(CONNECTED_VEHICLE_TYPES))
			{
				this.connectedCarsObserver.addVehicle(vehicle);
			}
			*/
		}
		
	}

	@Override
	public void vehicleArrived(SUMOVehicle vehicle) {
		
		if(this.connection!=null && this.connectedCarsObserver!=null)
		{
			this.connectedCarsObserver.removeVehicle(vehicle.getID());
		}
		
	}

	@Override
	public void vehicleTeleportStarting(SUMOVehicle vehicle) {
		System.out.println("vehicle departed: "+vehicle.getID());
		if(this.connection!=null && this.connectedCarsObserver!=null)
		{
			this.connectedCarsObserver.addVehicle(vehicle);
			/*
			if(vehicle.getVehicleType().equals(CONNECTED_VEHICLE_TYPES))
			{
				this.connectedCarsObserver.addVehicle(vehicle);
			}
			*/
		}
		
	}

	@Override
	public void vehicleTeleportEnding(SUMOVehicle vehicle) {
		if(this.connection!=null && this.connectedCarsObserver!=null)
		{
			this.connectedCarsObserver.removeVehicle(vehicle.getID());
		}
		
	}

}
